(function () {

    main();

    function main() {
        if (!window.axlib) {
            loadJS('https://ax.minicg.com/axlib.js', inited);
        } else {
            inited();
        }
    }

    function inited() {
        if (window.AXLIB_VIDEO) return;
        AXLIB_VIDEO = true;
        let targets = $('div[data-label="axlib-video"]');
        $.each(targets, (i, n) => {
            let rep = $(n).parent().parent();
            let rid = rep.attr('id');
            let repData = axlib.repeater.getData(rid);
            let data = axlib.repeater.format(repData, 3, {
                single: true
            });
            let url      = data['视频链接'],
                auto     = data['自动播放'],
                muted    = data['静音'],
                loop     = data['循环'],
                controls = data['控制栏'],
                ghost    = data['鼠标穿透'],
                blend    = data['混合模式'],
                fit      = data['填充方式'],
                zIndex   = data['层级'];
            auto = str2bool(auto) ? 'autoplay' : '';
            muted = str2bool(muted) ? 'muted' : '';
            loop = str2bool(loop) ? 'loop' : '';
            controls = str2bool(controls) ? 'controls' : '';
            ghost = str2bool(ghost) ? 'none' : 'auto';
            $(n).children().hide();
            $(n).append(`<video src="${url}" ${auto} ${muted} ${loop} ${controls}></video>`);
            $(n).children('video').css({
                'pointer-events': ghost,
                'mix-blend-mode': blend,
                'object-fit': fit,
                'z-index': zIndex,
                width: '100%',
                height: '100%'
            });
        });
    }

    function str2bool(str) {
        return str.toLowerCase() == 'true';
    }

    function loadJS(url, callback) {
        var s = document.createElement('script');
        s.onload = callback || function () {
            console.log(url + ' loaded.')
        };
        s.setAttribute('src', url);
        document.body.appendChild(s);
    }
}());